import { CreateMenuDto, UpdateMenuDto } from "@/dto/menu.dto";
import MenuService from "@/services/menu.service";
import { validateDto } from "@/utils/validateDto";
import { Context } from "koa";

class MenuController {
    // 添加菜单
    async add(ctx: Context) {
        const reqData = ctx.request.body as CreateMenuDto;
        const errors = await validateDto(CreateMenuDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }

        const menu = await MenuService.create(reqData);
        ctx.body = {
            code: 200,
            message: "详情查询成功",
            data: menu,

        };
    }
    async update(ctx: Context) {
        const reqData = ctx.request.body as UpdateMenuDto;
        const errors = await validateDto(UpdateMenuDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }

        const menu = await MenuService.update(reqData);
        ctx.body = {
            code: 200,
            message: "更新成功",
            data: menu,

        };
    }
    // 获取树形菜单
    async tree(ctx: Context) {
        const reqData = ctx.request.body as { name?: string };
        ctx.body = {
            code: 200,
            message: "详情查询成功",
            data: await MenuService.getTree(reqData),

        };
    }
    // 获取扁平菜单
    async flat(ctx: Context) {
        ctx.body = {
            code: 200,
            message: "详情查询成功",
            data: await MenuService.getFlatTree(),

        };
    }
    // 删除菜单
    async delete(ctx: Context) {        
        const reqData = ctx.request.body as {id:number};
        await MenuService.delete(Number(reqData.id));
            ctx.body = {
            code: 200,
            message: "删除成功",
            data: {},

        };

    }


}
export default new MenuController()